package com.test.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.test.dao.UserDao;
import com.test.model.UserInfo;

@Service
public class UserServiceImpl implements UserService
{
	@Resource
	UserDao dao = null;
	
	@Override
	public UserInfo getUser(Integer id)
	{
		
		String sql = "select * from user_info where id = ? ";
		return dao.findBean(sql, id); 
	}

	@Override
	public List<UserInfo> getAllUsers()
	{
		String sql = "select * from user_info where role = ?";
		List<UserInfo> users = dao.findBeanList(sql, 1);
		return users;
	}
	
	@Override
	public List<Map<String, Object>> getAllUsersMap()
	{
		String sql = "select * from user_info";
		return dao.findMapList(sql);
	}

	@Override
	public List<UserInfo> getUsersByRole(Integer role){
		String sql = "select * from user_info where role = ?";
		return dao.findBeanList(sql, role);
	}
	
	@Override
	public Boolean addUser(UserInfo user)
	{
		Boolean rs = false;
		String sf = null;
		try
		{
			//String sql = "INSERT INTO user_info(account, password, nickname, role, regtime, qq) VALUES(?,?,?,?,?,?);";
			//rs = dao.insert(sql, new Object[]{user.getAccount(),user.getPassword(),user.getNickname(),user.getRole(),new Date(),"9352745145"});
			user.setAccount("Lucy");
			updateUser(user);
			
			System.out.println(sf.toString());
			
			user.setAccount("Lily");
			updateUser(user);
			
			
		}
		catch(NullPointerException e)
		{
			System.out.println("Exception:"+e.getMessage());
		}
		return rs;
	}

	@Override
	public Boolean updateUser(UserInfo User)
	{
		String sql = "update user_info set nickname = ? where account = ? ";
		return dao.update(sql, new Object[]{User.getNickname(),User.getAccount()});
	}

}
